package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.filecenter.domain.po.tableinfohistory.TableInfoHistoryPo;
import com.bwda.dsrs.filecenter.domain.condition.tableinfohistory.TableInfoHistoryCondition;
import com.bwda.dsrs.filecenter.domain.form.tableinfohistory.TableInfoHistoryQueryForm;
import com.bwda.dsrs.filecenter.domain.form.tableinfohistory.TableInfoHistoryUpdateForm;
import com.bwda.dsrs.filecenter.domain.form.tableinfohistory.TableInfoHistoryCreateForm;
import com.bwda.dsrs.filecenter.domain.vo.tableinfohistory.TableInfoHistoryVo;
import com.bwda.dsrs.filecenter.service.TableInfoHistoryService;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import java.util.List;
import java.util.ArrayList;
import com.bwda.dsrs.base.util.CopyUtil;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang.math.RandomUtils;
/**
 * @author Administrator
 */
@RestController
@RequestMapping("/tableinfohistory")
@Api(value = "/tableinfohistory", tags = "表的历史记录")
public class TableInfoHistoryController extends BaseController {

	@Autowired
	private TableInfoHistoryService tableInfoHistoryService;

	@ApiOperation(value = "查询表的历史记录",notes = "查询表的历史记录",httpMethod = "GET")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<TableInfoHistoryVo> query(@ApiParam(value = "id", required = true)@RequestParam Integer tableInfoHistoryId) throws BusinessServiceException {
		TableInfoHistoryPo po = tableInfoHistoryService.queryWithValid(tableInfoHistoryId);
		TableInfoHistoryVo vo = CopyUtil.transfer(po, TableInfoHistoryVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "查询表的历史记录数量",notes = "查询表的历史记录数量",httpMethod = "GET")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid TableInfoHistoryQueryForm form) throws BusinessServiceException {
		TableInfoHistoryCondition condition = this.getConditionByQueryForm(form);
		int count = tableInfoHistoryService.queryCount(condition);
		return getSuccessResult(count);
	}

	@ApiOperation(value = "查询表的历史记录列表",notes = "查询表的历史记录列表",httpMethod = "GET")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<TableInfoHistoryVo>> queryList(@ModelAttribute@Valid TableInfoHistoryQueryForm form) throws BusinessServiceException {
		TableInfoHistoryCondition condition = this.getConditionByQueryForm(form);
		List<TableInfoHistoryPo> poList = tableInfoHistoryService.queryList(condition);
		List<TableInfoHistoryVo> voList = CopyUtil.transfer(poList, TableInfoHistoryVo.class);
		return getSuccessResult(getListResponse(voList));
	}

	@ApiOperation(value = "查询表的历史记录列表(带分页)",notes = "查询表的历史记录列表(带分页)",httpMethod = "GET")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<TableInfoHistoryVo>> queryPageList(@ModelAttribute@Valid TableInfoHistoryQueryForm form) throws BusinessServiceException {
		TableInfoHistoryCondition condition = this.getConditionByQueryForm(form);
		List<TableInfoHistoryVo> voList = new ArrayList<>();
		int count = tableInfoHistoryService.queryCount(condition);
		if (count > 0) {
			List<TableInfoHistoryPo> poList = tableInfoHistoryService.queryList(condition);
			voList = CopyUtil.transfer(poList, TableInfoHistoryVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}

	@ApiOperation(value = "新增表的历史记录",notes = "新增表的历史记录",httpMethod = "POST")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<TableInfoHistoryVo> add(@ModelAttribute@Valid TableInfoHistoryCreateForm form) throws BusinessServiceException {
		TableInfoHistoryPo po = CopyUtil.transfer(form, TableInfoHistoryPo.class);
//		po.setTableInfoHistoryId(RandomUtils.nextInt());
		tableInfoHistoryService.insert(po);
		TableInfoHistoryVo vo = CopyUtil.transfer(po, TableInfoHistoryVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "修改表的历史记录",notes = "修改表的历史记录",httpMethod = "POST")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid TableInfoHistoryUpdateForm form) throws BusinessServiceException {
		TableInfoHistoryPo po = CopyUtil.transfer(form, TableInfoHistoryPo.class);
		tableInfoHistoryService.update(po);
		return getSuccessResult();
	}

	@ApiOperation(value = "删除表的历史记录",notes = "删除表的历史记录",httpMethod = "POST")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "id", required = true)@RequestParam Integer tableInfoHistoryId) throws BusinessServiceException {
		tableInfoHistoryService.delete(tableInfoHistoryId);
		return getSuccessResult();
	}

	/**
	 * TableInfoHistoryQueryForm转换为TableInfoHistoryCondition
	 *
	 * @param form
	 * @return
	 */
	private TableInfoHistoryCondition getConditionByQueryForm(TableInfoHistoryQueryForm form) {
		TableInfoHistoryCondition condition = CopyUtil.transfer(form, TableInfoHistoryCondition.class);
		return condition;
	}

}